/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.framework.test;

import axil.api.Context;
import axil.api.access.Accessible;
import axil.api.access.Accessor;

import static axil.framework.Functions.abort;


/**
 * A sample host application object representing a customer.
 */
public class Customer implements Accessible {
	private int id;
	private String firstName;
	private String lastName;


	public Customer(int id, String firstName, String lastName) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}


	public int getId() {
		return id;
	}


	public String getFirstName() {
		return firstName;
	}


	public String getLastName() {
		return lastName;
	}


	/**
	 * Get an accessor for the given field residing within this object. If the
	 * field cannot be found, then an exception is thrown.
	 *
	 * @param context
	 * 	The context being used for the access.
	 *
	 * @param field
	 * 	The name of the field as it appears in the Axil script. Note that Axil
	 * 	supports a highly flexible identifier mechanism, allowing spaces and
	 * 	punctuation in identifiers. It is the responsibility of the host
	 * 	application to define how identifiers from Axil are mapped to actual
	 * 	object fields.
	 *
	 * @return
	 * 	Returns a non-null accessor object. The returned object will be retained
	 * 	indefinitely by the caller and reused for subsequent accesses to the
	 * 	same field for objects of this type.
	 */
	public Accessor accessor(Context context, String field) {
		if (field.equals("first-name"))	return new AC(Field.first);
		if (field.equals("last-name"))	return new AC(Field.last);
		if (field.equals("id"))			return new AC(Field.cid);
		return null;
	}


	private enum Field {
		cid,
		first,
		last
	}

	/**
	 * A silly accessor, but it demonstrates that Axil can operate entirely
	 * without secret knowledge of its own accessor mechanisms.
	 */
	private static final class AC implements Accessor {
		private Field field;

		private AC(Field field) {
			this.field = field;
		}


		/**
		 * Access the field or object represented by this accessor object.
		 */
		public Object access(Context context, Object parent) {
			Customer c = (Customer)parent;
			switch(field) {
			case cid:
				return c.getId();
			case first:
				return c.getFirstName();
			case last:
				return c.getLastName();
			default:
				throw abort("Cannot get there from here.");
			}
		}
	}
}
